import {routerRedux} from 'dva/router';
import {query, update, create, remove} from '../services/user';
import {parse} from 'qs';

export default {
    namespace: 'user',
    state: {
        loading: false,
        activeKey: '',   //值为空关闭折叠板,'1'打开折叠板
        keyword: '',    //筛选
        list: [],    //列表数据
        total: null,    //总的记录
        current: 1, //当前页数
        modalVisible: false, //
        currentItem: {},
        modalType: 'create', //modal类型
    },
    subscriptions: {
        setup({dispatch, history}) {
            history.listen(location => {
                if (location.pathname === '/user') {
                    dispatch({
                        type: 'query',
                        payload: location.query,
                    });
                    dispatch({
                        type: 'aside/updateSelectKey',
                        payload: {selectedKeys: ['user']}
                    })
                }
            });
        },
    },
    effects: {
        *query({payload}, {call, put}) {
            yield put({type: 'showLoading'});
            let temp = {page: 1, keyword: '', ...payload};
            if (payload.keyword) {//url中有keyword则打开折叠板
                temp = {page: 1, keyword: '', ...payload, activeKey: '1'};
            }
            yield put({
                type: 'updateQueryKey',
                payload: temp,
            });
            const {data} = yield call(query, payload);
            if (data) {
                yield put({
                    type: 'querySuccess',
                    payload: {
                        list: data.results,
                        total: data.total,
                        current: data.current,
                    },
                });
            }
        },
        *update({payload}, {select, call, put}) {
            yield put({type: 'hideModal'});
            yield put({type: 'showLoading'});
            const id = yield select(({user}) => user.currentItem.id);
            const newUser = {...payload, id};
            const {data} = yield call(update, newUser);
            if (data) {
                yield put({
                    type: 'updateSuccess',
                    payload: newUser,
                });
            }
        },
        *create({payload}, {call, put}) {
            yield put({type: 'hideModal'});
            yield put({type: 'showLoading'});
            const {data} = yield call(create, payload);
            if (data) {
                yield put({type: 'createSuccess'});
                routerRedux.push({
                    pathname: '/user',
                    query: {page: 1},
                })
            }
        },
        *'delete'({payload}, {call, put}) {
            yield put({type: 'showLoading'});
            const {data} = yield call(remove, {id: payload});
            yield put({
                type: 'deleteSuccess',
                payload,
            });
        },
    },
    reducers: {
        showLoading(state) {
            return {...state, loading: true};
        },
        updateQueryKey(state, action){//更新搜索值
            return {...state, ...action.payload};
        },
        querySuccess(state, action) {
            return {...state, ...action.payload, loading: false};
        },
        showModal(state, action) {
            return {...state, ...action.payload, modalVisible: true};
        },
        hideModal(state) {
            return {...state, modalVisible: false};
        },
        updateSuccess(state, action) {
            const updateUser = action.payload;
            const newList = state.list.map(user => {
                if (user.id === updateUser.id) {
                    return {...user, ...updateUser};
                }
                return user;
            });
            return {...state, list: newList, loading: false};
        },
        createSuccess(state, action) {
            return {...state, ...action.payload, loading: false};
        },
        deleteSuccess(state, action) {
            const id = action.payload;
            const newList = state.list.filter(user => user.id !== id);
            return {...state, list: newList, loading: false};
        },
    },

};
